import { NCellRender} from '../../../types/cell-render.interface';
import './text-editor.less';

export class TextEditor extends NCellRender.AEdit{
    protected inputEle: HTMLInputElement;
    constructor(row: number, col: number) {
        super(row, col);
    }
    public initDom(ele: HTMLElement) {
        this.inputEle = document.createElement('input');
        this.inputEle.setAttribute('class', 'z-table-text-editor-input')
        ele.append(this.inputEle);
        
    }
    public initEdit(value: string) {
        this.inputEle.value = value ? value : '';
        this.inputEle.focus();
        this.inputEle.addEventListener('keydown', this.keydownListener);
    }
    protected keydownListener = (e: KeyboardEvent) => {
        if(e.key === 'Enter') {
            this.updateValue();
        }
    }
    protected updateValue() {
        this.finish(this, this.inputEle.value);
    }
    public blur() {
        this.updateValue();
    }
    public destroy() {
        this.inputEle.removeEventListener('keydown', this.keydownListener);
        this.finish = () => {
            
        }
    }
}